var network = require("../../../utils/request.js"); //网络请求

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoging: false,
    items: [],//列表数据
    proList: [],//商品图
    transmitImg: "../../../resource/images/transmit.png",//转发分享图标
    toLikeImg: "../../../resource/images/toLike.png",//去点赞图标
    likeImg: "../../../resource/images/like.png",//已点赞图标
    callImg: "../../../resource/images/call.png",//呼叫图标
    userInfo: [],//用户信息
  },

  //滑动获取选中商品
  getSelectItem: function (e) {
    var that = this;
    //每个商品的宽度 
    var itemWidth = e.detail.scrollWidth / that.data.proList.length;
    //滚动宽度
    var scrollLeft = e.detail.scrollLeft;
    //通过Math.round方法对滚动大于一半的位置进行进位
    var curIndex = Math.round(scrollLeft / itemWidth);
    for (var i = 0, len = that.data.proList.length; i < len; ++i) {
      that.data.proList[i].selected = false;
    }
    that.data.proList[curIndex].selected = true;
    that.setData({
      proList: that.data.proList
    });
  },

  //图片预览
  previewImage(e) {
    var img = e.currentTarget.dataset.url;
    //图片必须要http或https开头的才能预览。本地图片预览是黑屏效果
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img],
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = parseInt(options.id)
    this.getInfo(id)

    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  //获取详情信息
  getInfo(id) {
    network.post('getProvision/' + id).then(resp => {
      console.log(resp.rows)
      if (resp.rows.playType === 2) {
        resp.rows.playType = "账期"
      } else if (resp.rows.playType === 1) {
        resp.rows.playType = "现金"
      } else {
        resp.rows.playType = "其他"
      }
      if (resp.rows.status === '1') {
        resp.rows.status = "审核通过"
      }else if (resp.rows.status === '2') {
        resp.rows.status = "下架"
      }else if (resp.rows.status === '0') {
        resp.rows.status = "未审核"
      } else if (resp.rows.status === '-1') {
        resp.rows.status = "审核未通过"
      } else if (resp.rows.status === '-2') {
        resp.rows.status = "撤销"
      }else{
        resp.rows.status = "暂无"
      }
      this.setData({
        items: resp.rows,
        isLoging: false,
      })
    })
  },

  //呼叫按钮
  btnCall(e) {
    let phone = this.data.items.mobile
    let id = this.data.items.id
    if (this.data.userInfo.mobile == '') {
      wx.showToast({
        title: '请先登录！',
      })
    } else {
      //拨打电话
      wx.makePhoneCall({
        phoneNumber: phone,
        success: function () {
          //调用拨打电话的接口
          network.post("phoneProvision/" + id).then(resp => {

          })
        }
      })
    }

  },

  //点赞按钮
  btnLike() {
    let that = this
    let likeId = that.data.items.id
    network.post("likeProvision/" + likeId).then(resp => {
      if (resp.rows == 1) {
        wx.showToast({
          title: '点赞成功',
        })

        //修改页面点赞图标，等接口修改字段
        that.data.items.likeFlag = true
        that.data.items.likeNum = that.data.items.likeNum + 1
        that.setData({
          items: that.data.items
        })
      } else {
        wx.showToast({
          title: '您已经点过了',
          icon: 'none'
        })
      }
    })

  },

  //分享按钮
  btnShare() { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '跑合中药材供需云平台',
      imageUrl: '../../resource/images/share.jpg',
      path: 'pages/supplyList/supplyList',
      success: function (res) {
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享取消',
        })
      }
    }
  }
})